package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class Chaxun extends HttpServlet {
	private static final Log log=LogFactory.getLog(Chaxun.class);
	private cn.com.elite.FtpBean ftpbean;

	/**
	 * Constructor of the object.
	 */
	public Chaxun() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	
		String xml="";
		String event_guid=request.getParameter("event_guid");
		log.debug("event_guid:"+event_guid);
		String sql=" select * from record where event_guid='"+event_guid+"' ";
		try {
			List list=ftpbean.chaxun(sql);
			if(list!=null&&list.size()>0){
				Document result=util.getDoc();
				Element root=result.addElement("root");
				SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				for(int i=0;i<list.size();i++){
					Map map=(Map)list.get(i);
					
					Element root1=root.addElement("result");
					
					String recguid=util.getStr((String)map.get("record_guid"));
					log.debug("recguid:"+recguid);
					root1.addElement("rec_guid").addText(recguid);
					
					String ani=util.getStr((String)map.get("ani"));
					log.debug("ani:"+ani);
					root1.addElement("ani").addText(ani);
					
					String dnis=util.getStr((String)map.get("dnis"));
					log.debug("dnis:"+dnis);
					root1.addElement("dnis").addText(dnis);
					
				}
				xml=result.asXML();
			}
		} catch (Exception e) {
			log.info(util.getERRO(e));
		}
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        out.print(xml);	
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		try{
	        ServletContext application;   
	        WebApplicationContext wac;   
	        application = getServletContext();   
	        wac = WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
	        ftpbean = (cn.com.elite.FtpBean) wac.getBean("ftpbean");  
		}catch(Exception e){
			log.info(util.getERRO(e));
		}
	}

}
